package com.htgraph.renderer;

import com.ht.aems.util.RtDataBuffer;
import com.ht.aems.util.RtDataChangeListener;
import com.mxgraph.model.mxIGraphModel;
import com.htgraph.flash.Flasher;

/**
 * Created by IntelliJ IDEA.
 *
 * @author Dong Shufeng
 *         Date: 2010-11-24
 */
public abstract class CellRenderUpdator implements RtDataChangeListener {
    protected mxIGraphModel model;
    protected boolean isPaused = false;
    protected boolean isUpdating = false;
    Flasher flasher = Flasher.getDoNothingFlasher();

    public CellRenderUpdator(mxIGraphModel model) {
        this.model = model;
    }

    protected CellRenderUpdator() {
    }

    public void dataChanged(RtDataBuffer buffer) {
        try {
            if(isUpdating)
                return;
            isUpdating = true;
            if(!isPaused && model != null) {
                try {
                    model.beginUpdate();
                    updateCells(buffer);
                }finally{
                    model.endUpdate();
                }
            }
        } finally{
            isUpdating = false;
        }
    }

    public mxIGraphModel getModel() {
        return model;
    }

    public void setModel(mxIGraphModel model) {
        this.model = model;
    }

    public boolean isPaused() {
        return isPaused;
    }

    public void setPaused(boolean paused) {
        isPaused = paused;
    }

    public Flasher getFlasher() {
        return flasher;
    }

    public void setFlasher(Flasher flasher) {
        this.flasher = flasher;
    }

    abstract public void updateCells(RtDataBuffer buffer);
}
